﻿﻿var listData = {};
var rowid = 0;
var gridOption = null;
var mainQlmGrid = null;

//添加行
$(document).on("click",".addRow",function(){
	addRow({});
});

//删除行
 $(document).on("click",".deleteRow",function(){
	deleteRow($(this).parent().parent());
	if(gridOption.afterDelete){
		gridOption.afterDelete($(this));
	}
	if($(mainQlmGrid).find("._tr").length==0){
		addRow({});
	}
});
 
//spinner加一
$(document).on("click",".spinner-plus",function(){
	var name = $(this).parent().children("input").attr("name");
	var value = $(this).parent().children("input").val();
	var id = $(this).parent().parent("tr").attr("id");
	if(value!=""&&value!=null&&!isNaN(value)){
		value = parseFloat(value)+1;
	}else{
		value = 1
	}
	updateCell(name,value,id);
	if(gridOption.afterEdit){
		gridOption.afterEdit($(this).parent().children("input"));
	}
	return false;
});

//spinner减一
$(document).on("click",".spinner-minus",function(){
	var name = $(this).parent().children("input").attr("name");
	var value = $(this).parent().children("input").val();
	var id = $(this).parent().parent("tr").attr("id");
	if(value!=""&&value!=null&&!isNaN(value)){
		value = parseFloat(value)-1;
	}else{
		value = 1;
	}
	updateCell(name,value,id);
	if(gridOption.afterEdit){
		gridOption.afterEdit($(this).parent().children("input"));
	}
	return false;
});

/**
 * 渲染表格的行
 * @param data 渲染的行数据
 * @returns {String}
 */
function compileRow(data){
	listData["rows_"+rowid] = data;
	var html = "<tr class='_tr' id='rows_"+rowid+"'>";
	rowid++;
	if(gridOption.isNumber){
		var i = $(mainQlmGrid).find("._tr").length;
		html += "<td class='_td_num'>"+(++i)+"</td>";
	}
	if(gridOption.isChecked){
		html += "<td class='_td_check'></td>";
	}
	for ( var j = 0 ;j < gridOption.columns.length; j++ ){
	/*for(var j in gridOption.columns){*/
		var rows = (gridOption.columns)[j];
		var value = "";
		if(data.hasOwnProperty(rows.name)){
			value = data[rows.name];
			if(rows.render&&data){
				value = rows.render(data[rows.name],data);
			}
			if(rows.isHide){
				html += "<td class='_td_cell _td_cell_hide'>";
			}else{
				html += "<td class='_td_cell' width='"+rows.width+"'>";
			}
			if(rows.isNotInput){
				html += "<div name='"+rows.name+"'";
			}else{
				html += "<input type='text' name='"+rows.name+"' value='"+value+"'";
			}
		}else{
			if(rows.init){
				value = rows.init(value,data);
				if(rows.isHide){
					html += "<td class='_td_cell _td_cell_hide'>";
				}else{
					html += "<td class='_td_cell' width='"+rows.width+"'>";
				}
				if(rows.isNotInput){
					html += "<div name='"+rows.name+"'";
				}else{
					html += "<input type='text' name='"+rows.name+"' value='"+value+"'";
				}
			}else{
				if(rows.isHide){
					html += "<td class='_td_cell _td_cell_hide'>";
				}else{
					html += "<td class='_td_cell' width='"+rows.width+"'>";
				}
				if(rows.isNotInput){
					html += "<div name='"+rows.name+"'";
				}else{
					html += "<input type='text' name='"+rows.name+"'";
				}
			}
		}
		if(rows.type){
			html += " class='"+rows.type+"'";
		}
		if(!rows.isEdit){
			if(rows.isNotInput){
				html += ">"+value+"</div></td>";
			}else{
				html += " readonly='readonly'/></td>";
			}
		}else{
			if(rows.isNotInput){
				html += ">"+value+"</div>";
			}else{
				html += "/>";
			}
			if(rows.type == "spinner"){
				html += "<i class='fa fa-plus spinner-plus'></i><i class='fa fa-minus spinner-minus'></i></td>";
			}else{
				html += "</td>";
			}
		}
	}
	html += "<td class='_td_cell _td_operate'><div class='addRow'></div><div class='deleteRow'></div></td></tr>";
	return html;
}

/**
 * 初始化渲染表格
 * @param data
 * @param option
 * @returns {String}
 */
function compile(obj,data,option){
	//表格头部
	if(!option){
		return false;
	}
	gridOption = option;
	mainQlmGrid = obj;
	var thead = "<thead><tr class='_tr_head'>";
	if(option.isNumber){
		thead += "<td class='_td_num'></td>";
	}
	if(option.isChecked){
		thead += "<td class='_td_check all_checked'></td>";
	}
	for ( var j = 0 ;j < option.columns.length; j++ ){
	/*for(var j in option.columns){*/
		var rows = (option.columns)[j];
		if(rows.isHide){
			thead += "<td class='_td_head _te_head_hide'>"+rows.display+"</td>";
		}else{
			thead += "<td class='_td_head' width='"+rows.width+"'>"+rows.display+"</td>";
		}
	}
	thead += "<td class='_td_head _td_operate'></td></tr></thead>";
	
	//表格内容
	var tbody = "<tbody>";
	for(var i=0;i<data.length;i++){
		var oddClass = i%2>0?"_td_odd":"";
		var item = data[i];
		if(JSON.stringify(item)!=null&&JSON.stringify(item)!="{}"&&JSON.stringify(item)!="undefined"&&JSON.stringify(item)!=""){
			listData["rows_"+rowid] = item;
		}
		tbody += "<tr class='_tr "+oddClass+"' id='rows_"+rowid+"'>";
		rowid++;
		if(option.isNumber){
			tbody += "<td class='_td_num'>"+(i+1)+"</td>";
		}
		if(option.isChecked){
			tbody += "<td class='_td_check'></td>";
		}
		for ( var j = 0 ;j < option.columns.length; j++ ){
			var rows = (option.columns)[j];
			var value = "";
			if(item.hasOwnProperty(rows.name)){
				value = item[rows.name];
				if(rows.render&&item){
					value = rows.render(item[rows.name],item);
				}
				if(rows.isHide){
 					tbody += "<td class='_td_cell _td_cell_hide'>";
 				}else{
 					tbody += "<td class='_td_cell' width='"+rows.width+"'>";
 				}
				if(rows.isNotInput){
					tbody += "<div name='"+rows.name+"'";
				}else{
					tbody += "<input type='text' name='"+rows.name+"' value='"+value+"'";
				}
			}else{
				if(rows.init){
					value = rows.init(value,item);
					if(rows.isHide){
	 					tbody += "<td class='_td_cell _td_cell_hide'>";
	 				}else{
	 					tbody += "<td class='_td_cell' width='"+rows.width+"'>";
	 				}
					if(rows.isNotInput){
						tbody += "<div name='"+rows.name+"'";
					}else{
						tbody += "<input type='text' name='"+rows.name+"' value='"+value+"'";
					}
				}else{
					if(rows.isHide){
	 					tbody += "<td class='_td_cell _td_cell_hide'>";
	 				}else{
	 					tbody += "<td class='_td_cell' width='"+rows.width+"'>";
	 				}
					if(rows.isNotInput){
						tbody += "<div name='"+rows.name+"'";
					}else{
						tbody += "<input type='text' name='"+rows.name+"'";
					}
				}
			}
			if(rows.type){
				tbody += " class='"+rows.type+"'";
			}
			if(!rows.isEdit){
				if(rows.isNotInput){
					tbody += ">"+value+"</div></td>";
				}else{
					tbody += " readonly='readonly'/></td>";
				}
			}else{
				if(rows.isNotInput){
					tbody += ">"+value+"</div>";
				}else{
					tbody += "/>";
				}
				if(rows.type == "spinner"){
					tbody += "<i class='fa fa-plus spinner-plus'></i><i class='fa fa-minus spinner-minus'></i></td>";
				}else{
					tbody += "</td>";
				}
			}
		}
		tbody += "<td class='_td_cell _td_operate'><div class='addRow'></div><div class='deleteRow'></div></td></tr>";
	}
	tbody += "</tbody>";
	return "<table class='grid_table'>"+thead+tbody+"</table>";
}


/**
 * 初始化渲染表格
 * @param data
 * @param option
 * @returns {String}
 */
function compileNotOperate(obj,data,option){
	//表格头部
	if(!option){
		return false;
	}
	gridOption = option;
	mainQlmGrid = obj;
	var thead = "<thead><tr class='_tr_head'>";
	if(option.isNumber){
		thead += "<td class='_td_num'></td>";
	}
	if(option.isChecked){
		thead += "<td class='_td_check all_checked'></td>";
	}
	for ( var j = 0 ;j < option.columns.length; j++ ){
	/*for(var j in option.columns){*/
		var rows = (option.columns)[j];
		if(rows.isHide){
			thead += "<td class='_td_head _te_head_hide'>"+rows.display+"</td>";
		}else{
			thead += "<td class='_td_head' width='"+rows.width+"'>"+rows.display+"</td>";
		}
	}
	thead += "<td class='_td_head _td_operate'></td></tr></thead>";
	
	//表格内容
	var tbody = "<tbody>";
	for(var i=0;i<data.length;i++){
		var item = data[i];
		if(JSON.stringify(item)!=null&&JSON.stringify(item)!="{}"&&JSON.stringify(item)!="undefined"&&JSON.stringify(item)!=""){
			listData["rows_"+rowid] = item;
		}
		tbody += "<tr class='_tr' id='rows_"+rowid+"'>";
		rowid++;
		if(option.isNumber){
			tbody += "<td class='_td_num'>"+(i+1)+"</td>";
		}
		if(option.isChecked){
			tbody += "<td class='_td_check'></td>";
		}
		for ( var j = 0 ;j < option.columns.length; j++ ){
			var rows = (option.columns)[j];
			var value = "";
			if(item.hasOwnProperty(rows.name)){
				value = item[rows.name];
				if(rows.render&&item){
					value = rows.render(item[rows.name],item);
				}
				if(rows.isHide){
 					tbody += "<td class='_td_cell _td_cell_hide'>";
 				}else{
 					tbody += "<td class='_td_cell' width='"+rows.width+"'>";
 				}
				if(rows.isNotInput){
					tbody += "<div name='"+rows.name+"'";
				}else{
					tbody += "<input type='text' name='"+rows.name+"' value='"+value+"'";
				}
			}else{
				if(rows.init){
					value = rows.init(value,item);
					if(rows.isHide){
	 					tbody += "<td class='_td_cell _td_cell_hide'>";
	 				}else{
	 					tbody += "<td class='_td_cell' width='"+rows.width+"'>";
	 				}
					if(rows.isNotInput){
						tbody += "<div name='"+rows.name+"'";
					}else{
						tbody += "<input type='text' name='"+rows.name+"' value='"+value+"'";
					}
				}else{
					if(rows.isHide){
	 					tbody += "<td class='_td_cell _td_cell_hide'>";
	 				}else{
	 					tbody += "<td class='_td_cell' width='"+rows.width+"'>";
	 				}
					if(rows.isNotInput){
						tbody += "<div name='"+rows.name+"'";
					}else{
						tbody += "<input type='text' name='"+rows.name+"'";
					}
				}
			}
			if(rows.type){
				tbody += " class='"+rows.type+"'";
			}
			if(!rows.isEdit){
				if(rows.isNotInput){
					tbody += ">"+value+"</div></td>";
				}else{
					tbody += " readonly='readonly'/></td>";
				}
			}else{
				if(rows.isNotInput){
					tbody += ">"+value+"</div>";
				}else{
					tbody += "/>";
				}
				if(rows.type == "spinner"){
					tbody += "<i class='fa fa-plus spinner-plus'></i><i class='fa fa-minus spinner-minus'></i></td>";
				}else{
					tbody += "</td>";
				}
			}
		}
		tbody += "</tr>";
	}
	tbody += "</tbody>";
	return "<table class='grid_table'>"+thead+tbody+"</table>";
}

/**
 * 删除当前表格的某一行
 * @param gridObj 当前表格
 * @param obj 某一行
 */
function deleteRow(obj){
	var rowid = $(obj).attr("id");
	delete listData[rowid];
	$(obj).remove();
	var rowindex = 1;
	$(mainQlmGrid).find("._tr").each(function(){
		$(this).find("._td_num").text(rowindex);
		rowindex++;
	});
}

/**
 * 在当前表格最后一行插入行
 * @param obj 当前表格
 * @param data 插入的数据
 * @param option 该渲染表格的行格式
 */
function addRow(data,callback){
	$(mainQlmGrid).find("tbody").append(compileRow(data));
	try {
		render();
	} catch (e) {
	}
	if(callback){
		callback();
	}
}

/**
 * 在当前表格最后一行插入行
 * @param obj 当前表格
 * @param data 插入的数据
 * @param option 该渲染表格的行格式
 */
function addRows(datas,callback){
	for(var i=0;datas.length>i;i++){
		$(mainQlmGrid).find("tbody").append(compileRow(datas[i]));
	}
	try {
		render();
	} catch (e) {
	}
	if(callback){
		callback();
	}
}

/**
 * 在当前表格第一行插入行
 * @param obj 当前表格
 * @param data 插入的数据
 * @param option 该渲染表格的行格式
 */
function addPreRow(data,callback){
	$(mainQlmGrid).find("tbody").prepend(compileRow(data));
	render();
	var rowindex = 1;
	$(mainQlmGrid).find("._tr").each(function(){
		$(this).find("._td_num").text(rowindex);
		rowindex++;
	});
	if(callback){
		callback();
	}
}

/**
 * 更新单元格
 * @param obj 当前表格
 * @param cellName 单元格的名称
 * @param value 单元格的值
 * @param data 某一行
 */
function updateCell(cellName,value,data){
	if($("#"+data)&&$("#"+data).length>0){
		if(listData.hasOwnProperty(data)){
			(listData[data])[cellName] = value;
		}else{
			listData[data] = {};
			listData[data][cellName] = value;
		}
		$("#"+data).find("input[name='"+cellName+"']").addClass("edited-td");
		$("#"+data).find("input[name='"+cellName+"']").val(value);
	}
}

/**
 * 勾选多选框事件
 */
$(document).on("click","._td_check",function(){
	if($(this).hasClass("_td_checked")){
		$(this).removeClass("_td_checked");
		if($(this).hasClass("all_checked")){
			$("._td_check").removeClass("_td_checked");
		}
	}else{
		$(this).addClass("_td_checked");
		if($(this).hasClass("all_checked")){
			$("._td_check").addClass("_td_checked");
		}
	}
	$("._tr ._td_check").each(function(){
		if($(this).hasClass("_td_checked")){
			$(this).parent().addClass("_td_cell_checked");
		}else{
			$(this).parent().removeClass("_td_cell_checked");
		}
	});
	return false;
});

/**
 * 选中行事件
 */
$(document).on("click","._tr",function(e){
	var obj = $(this).find("._td_check");
	if($(obj).hasClass("_td_checked")){
		$(obj).removeClass("_td_checked");
		$(this).removeClass("_td_cell_checked");
	}else{
		$(obj).addClass("_td_checked");
		$(this).addClass("_td_cell_checked");
	}
	return false;
});
	
/**
 * 点击可编辑的单元格，添加样式
 */
$(document).on("click","._td_cell input",function(){
	if($(this).attr("readonly")!="readonly"){
		$(this).addClass("focus_input");
 		return false;
	}
});

/**
 * 可编辑的单元格失去焦点，去除样式
 */
$(document).on("blur","._td_cell input",function(){
	$(this).removeClass("focus_input");
});

$(document).on("change","._td_cell input",function(){
	if(gridOption.afterEdit){
		gridOption.afterEdit($(this));
	}
});
	
/**
 * 回车或tab键切换文本框输入，并可以添加下一行
 */
$(document).on("keydown","._td_cell input",function(event){
	var keyCode = event.keyCode?event.keyCode:event.which;
	if(!flag && (keyCode ==13 || keyCode == 9) && !event.altKey && !event.ctrlKey){
		focusOnNext($(this).parent(),$(this).parent().next("td"));
		return false;
	}
});

function focusOnNext(prevObj,obj){
	if($(obj).length>0){
		var _input = $(obj).children("input");
 		if(_input&&_input.length>0){
 			if(_input.attr("readonly")!="readonly"&&_input.attr("type")=="text"){
 				$(_input).focus();
 				$(_input).click();
 				return false;
 			}else{
 				focusOnNext($(obj),$(obj).next());
 			}
 		}else{
 			focusOnNext($(obj),$(obj).next());
 		}
	}else{
		var _tr = $(prevObj).parent().next("tr");
		if(_tr&&_tr.length>0){
			focusOnNext($(prevObj),$(_tr).children("td:eq(0)"));
		}else{
			addRow({});
			var _tr = $(prevObj).parent().next("tr");
			if(_tr&&_tr.length>0){
 				focusOnNext($(prevObj),$(_tr).children("td:eq(0)"));
 			}
		}
	}
	return false;
}

/**
 * 是否显示该列
 * @param cellName
 * @param isShow
 */
function toggleCol(cellName,isShow){
	/*for(var j in gridOption.columns){*/
	for ( var j = 0 ;j < gridOption.columns.length; j++ ){
		var rows = (gridOption.columns)[j];
		if(rows.name==cellName){
			(gridOption.columns)[j].isHide = !isShow;
			if(isShow){
				$("._td_head:eq("+j+")").removeClass("_te_head_hide");
				$(mainQlmGrid).find("input[name='"+cellName+"']").each(function(){
					$(this).parent().removeClass("_td_cell_hide");
				});
			}else{
				$("._td_head:eq("+j+")").addClass("_te_head_hide");
				$(mainQlmGrid).find("input[name='"+cellName+"']").each(function(){
					$(this).parent().addClass("_td_cell_hide");
				});
			}
		}
	}
}




/**
 * 初始化渲染表格 无操作项目 add by Lin
 * @param data
 * @param option
 * @returns {String}
 */
function compileChooseOperate(obj,data,option){
	var operateadd = false;
	var operatedel = false;
	
	//表格头部
	if(!option){
		return false;
	}
	gridOption = option;
	mainQlmGrid = obj;
	
	if(option.operateadd != undefined){
		operateadd = option.operateadd;
	}
	if(option.operatedel != undefined){
		operatedel = option.operatedel;
	}
	
	var thead = "<thead><tr class='_tr_head'>";
	if(option.isNumber){
		thead += "<td class='_td_num'></td>";
	}
	if(option.isChecked){
		thead += "<td class='_td_check all_checked'></td>";
	}
	for ( var j = 0 ;j < option.columns.length; j++ ){
	/*for(var j in option.columns){*/
		var rows = (option.columns)[j];
		if(rows.isHide){
			thead += "<td class='_td_head _te_head_hide'>"+rows.display+"</td>";
		}else{
			thead += "<td class='_td_head' width='"+rows.width+"'>"+rows.display+"</td>";
		}
	}
	
	if(operateadd||operatedel)
		thead += "<td class='_td_head _td_operate'></td></tr></thead>";
	else
		thead += "</thead>";
	
	//表格内容
	var tbody = "<tbody>";
	for(var i=0;i<data.length;i++){
		var item = data[i];
		if(item&&item.length>0){
			listData["rows_"+rowid] = item;
		}
		tbody += "<tr class='_tr' id='rows_"+rowid+"'>";
		rowid++;
		if(option.isNumber){
			tbody += "<td class='_td_num'>"+(i+1)+"</td>";
		}
		if(option.isChecked){
			tbody += "<td class='_td_check'></td>";
		}
		for ( var j = 0 ;j < option.columns.length; j++ ){
		/*for(var j in option.columns){*/
			var rows = (option.columns)[j];
			if(item.hasOwnProperty(rows.name)){
				var value = item[rows.name]
				if(rows.render){
				value = rows.render(item[rows.name]);
			}
				if(rows.isHide){
 					tbody += "<td class='_td_cell _td_cell_hide'><input type='text' name='"+rows.name+"' value='"+value+"'"
 				}else{
 					tbody += "<td class='_td_cell' width='"+rows.width+"'><input type='text' name='"+rows.name+"' value='"+value+"'"
 				}
			}else{
				if(rows.isHide){
 					tbody += "<td class='_td_cell _td_cell_hide'><input type='text' name='"+rows.name+"'"
 				}else{
 					tbody += "<td class='_td_cell' width='"+rows.width+"'><input type='text' name='"+rows.name+"'"
 				}
			}
			if(rows.type){
				tbody += " class='"+rows.type+"'";
			}
			if(!rows.isEdit){
				tbody += " readonly='readonly'/></td>";
			}else{
				tbody += "/></td>";
			}
		}
		
		if(operateadd && operatedel){
			tbody += "<td class='_td_cell _td_operate'><div class='addORow' ></div><div class='deleteORow'></div></td></tr>";
		}else if(operateadd && !operatedel){
			tbody += "<td class='_td_cell _td_operate'><div class='addORow' ></div></td></tr>";
		}else if(!operateadd && operatedel){
			tbody += "<td class='_td_cell _td_operate'><div class='deleteORow'></div></td></tr>";
		}
	}
	tbody += "</tbody>";
	return "<table class='grid_table'>"+thead+tbody+"</table>";
}

/**
 * 渲染表格的行
 * @param data 渲染的行数据
 * @returns {String}
 */
function compileRowChooseOperate(option,data){
	var operateadd = false;
	var operatedel = false;
	
	gridOption = option;
	
	if(option.operateadd != undefined){
		operateadd = option.operateadd;
	}
	if(option.operatedel != undefined){
		operatedel = option.operatedel;
	}
	
	listData["rows_"+rowid] = data;
	var html = "<tr class='_tr' id='rows_"+rowid+"'>";
	rowid++;
	if(gridOption.isNumber){
		var i = $(mainQlmGrid).find("._tr").length;
		html += "<td class='_td_num'>"+(++i)+"</td>";
	}
	if(gridOption.isChecked){
		html += "<td class='_td_check'></td>";
	}
	for ( var j = 0 ;j < gridOption.columns.length; j++ ){
	/*for(var j in gridOption.columns){*/
		var rows = (gridOption.columns)[j];
		if(data.hasOwnProperty(rows.name)){
			var value = data[rows.name]
			if(rows.render){
				value = rows.render(data[rows.name]);
			}
			if(rows.isHide){
				html += "<td class='_td_cell _td_cell_hide'><input type='text' name='"+rows.name+"' value='"+value+"'"
			}else{
				html += "<td class='_td_cell' width='"+rows.width+"'><input type='text' name='"+rows.name+"' value='"+value+"'"
			}
		}else{
			if(rows.isHide){
				html += "<td class='_td_cell _td_cell_hide'><input type='text' name='"+rows.name+"'" 
			}else{
				html += "<td class='_td_cell' width='"+rows.width+"'><input type='text' name='"+rows.name+"'"
			}
		}
		if(rows.type){
			html += " class='"+rows.type+"'";
		}
		if(!rows.isEdit){
			html += " readonly='readonly'/></td>";
		}else{
			html += "/></td>";
		}
	}
	
	if(operateadd && operatedel)
		html += "<td class='_td_cell _td_operate'><div class='addORow'></div><div class='deleteORow'></div></td></tr>";
	else if(operateadd && !operatedel)
		html += "<td class='_td_cell _td_operate'><div class='addORow'></div></td></tr>";
	else if(!operateadd && operatedel)
		html += "<td class='_td_cell _td_operate'><div class='deleteORow'></div></td></tr>";
	return html;
}
